#pragma once
#include <iostream>
#include <mysql/mysql.h>
#include <string>
#include "Log.hpp"

const std::string host = "127.0.0.1";
const std::string user = "root";
const std::string passwd = "1234560";
const std::string db = "parkdb";
const unsigned int port = 3306;

enum{
    initErr = 2,
    connectErr 
};


class SQL
{
public:
    SQL(): my_(nullptr)
    {
        my_ = mysql_init(nullptr);
    }
    ~SQL()
    {
        if (my_) {
            mysql_close(my_);
            my_ = nullptr;
        }
    }

    SQL(const SQL&) = delete;
    SQL& operator=(const SQL&) = delete;
    
    int connectSQL()
    {
        if (nullptr == my_)
        {
            std::cerr << "init MySQL error" << std::endl;
            exit(initErr);
        }
        if (mysql_real_connect(my_, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0) == nullptr)
        {
            std::cerr << "connect MySQL error" << std::endl;
            mysql_close(my_);  // 关闭初始化但连接失败的对象
            my_ = nullptr;
            exit(connectErr);
        }
        lg(Info,"connect MySQL success");
        std::cout << "connect MySQL success" << std::endl;
        return 1;
    }

public:
    MYSQL *my_;
};